import { useEffect, useRef } from 'react';

/**
 * Three.js场景的自定义Hook
 * 封装了常见的场景初始化和清理逻辑
 * 
 * @param initFunction - 场景初始化函数，接收container参数，返回cleanup函数
 * @returns mountRef - 用于绑定到DOM元素的ref
 */
export const useThreeScene = (
  initFunction: (container: HTMLElement) => (() => void)
) => {
  const mountRef = useRef<HTMLDivElement>(null);
  const cleanupRef = useRef<(() => void) | null>(null);

  useEffect(() => {
    if (!mountRef.current) return;

    // 调用纯Three.js初始化函数
    cleanupRef.current = initFunction(mountRef.current);

    // 组件卸载时清理
    return () => {
      if (cleanupRef.current) {
        cleanupRef.current();
      }
    };
  }, [initFunction]);

  return mountRef;
}; 